﻿using System;
using System.Windows;
using System.Windows.Controls;
using TaskClient2012.SystemService;
using TaskClient2012.UserService;

namespace TaskClient2012
{
    public partial class MainPage : UserControl
    {
        private UserServiceClient userServiceClient = new UserServiceClient();
        private SystemServiceClient systemServiceClient = new SystemServiceClient();

        public MainPage()
        {
            InitializeComponent();
            this.Loaded += OnMainPageLoaded;
        }

        async private void OnMainPageLoaded(object sender, RoutedEventArgs e)
        {
            var users = await userServiceClient.GetAllTaskAsync();
            dataGrid1.ItemsSource = users;
        }

        async private void OnGetTimeClick(object sender, RoutedEventArgs e)
        {
            var time = await systemServiceClient.GetSerivceTimeTaskAsync();
            MessageBox.Show(time.ToString());
        }

        async private void OnHiClick(object sender, RoutedEventArgs e)
        {
            var result = await systemServiceClient.SayHiToTaskAsync("Tom");
            MessageBox.Show(result);
        }

        async private void OnException1Click(object sender, RoutedEventArgs e)
        {
            var result = await systemServiceClient.SayHiToTaskAsync(null);
            MessageBox.Show(result);
        }

        async private void OnException2Click(object sender, RoutedEventArgs e)
        {
            var num = await systemServiceClient.GetNumTaskAsync(0);
            MessageBox.Show(num.ToString());
        }

        async private void OnMultiTaskClick(object sender, RoutedEventArgs e)
        {
            var num = await systemServiceClient.GetNumTaskAsync(3);
            var result = await systemServiceClient.SayHiToTaskAsync(num.ToString("n2"));
            var time = await systemServiceClient.GetSerivceTimeTaskAsync();
            var users = await userServiceClient.GetAllTaskAsync();
            MessageBox.Show(string.Format("Num:{1},{0}{2},{0}Time:{3},{0}UserCount:{4}", Environment.NewLine,
                               num.ToString("n2"),
                               result,
                               time.ToString(),
                               users.Count.ToString()));
        }
    }
}